package com.zczc.dao.api.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zczc.dao.api.MessageInfoDao;
import com.zczc.dao.mapper.MessageInfoMapper;
import com.zczc.entity.MessageInfo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * @Author:Mark
 * @Description:
 * @Date: Created on 14:48 2018-12-12
 * @Modify by:
 */
@Repository
public class MessageInfoDaoImpl implements MessageInfoDao {

  @Autowired
  private MessageInfoMapper messageInfoMapper;

  @Override
  public Integer addMessageInfo(MessageInfo messageInfo) {
    return messageInfoMapper.insertSelective(messageInfo);
  }

  @Override
  public Integer updateMessageInfoSelective(MessageInfo messageInfo) {
    return messageInfoMapper.updateByPrimaryKeySelective(messageInfo);
  }

  @Override
  public PageInfo<MessageInfo> selectMessageInfoByPage(MessageInfo messageInfo, Integer pageNum,
      Integer pageSize) {
    PageHelper.startPage(pageNum,pageSize);
    Example example=new Example(MessageInfo.class);
    Criteria criteria = example.createCriteria();
    criteria.andEqualTo("deleted",false);
    if(messageInfo!=null){
      if(messageInfo.getTitle()!=null){
        criteria.andLike("title","%"+messageInfo.getTitle()+"%");
      }if(messageInfo.getContent()!=null){
        criteria.andLike("content","%"+messageInfo.getContent()+"%");
      }if(messageInfo.getMessageCreater()!=null){
        criteria.andLike("messageCreater","%"+messageInfo.getMessageCreater()+"%");
      }if(messageInfo.getPushTime()!=null){
        criteria.andLessThan("pushTime",messageInfo.getPushTime());
      }if(messageInfo.getId()!=null){
        criteria.andEqualTo("id",messageInfo.getId());
      }
    }
    return PageInfo.of(messageInfoMapper.selectByExample(example));
  }

  @Override
  public Integer deleteMessageInfoByIdLogic(List<String> ids) {
    Example example=new Example(MessageInfo.class);
    Criteria criteria = example.createCriteria();
    criteria.andIn("id",ids);
    MessageInfo messageInfo=new MessageInfo();
    messageInfo.setDeleted(true);
    return messageInfoMapper.updateByExampleSelective(messageInfo,example);
  }

  @Override
  public MessageInfo selectMessageInfoById(String id) {
    return messageInfoMapper.selectByPrimaryKey(id);
  }

  @Override
  public List<MessageInfo> selectMessageInfoByIdAndTime(MessageInfo messageInfo) {
    Example example=new Example(MessageInfo.class);
    Criteria criteria = example.createCriteria();
    criteria.andEqualTo("id",messageInfo.getId());
    criteria.andLessThan("pushTime",messageInfo.getPushTime());
    return messageInfoMapper.selectByExample(example);
  }
}
